package com.puff.controller.platform;

import com.puff.dto.CustomerDTO;
import com.puff.dto.CustomerPageQueryDTO;
import com.puff.dto.ShopDTO;
import com.puff.dto.ShopPageQueryDTO;
import com.puff.result.PageResult;
import com.puff.result.Result;
import com.puff.service.CustomerService;
import com.puff.vo.CustomerVO;
import com.puff.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/platform/customer")
@Slf4j
@Api(tags = "客服管理相关接口")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 客服列表分页查询
     * @param customerPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "客服列表分页查询")
    public Result<PageResult> page(CustomerPageQueryDTO customerPageQueryDTO){
        log.info("客服列表分页查询:{}",customerPageQueryDTO);
        // 由于是query查询而不是json，因此不需要加@RequestBody
        PageResult pageResult = customerService.pageQuery(customerPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增客服
     * @param customerDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增客服")
    public Result save(@RequestBody CustomerDTO customerDTO){
        log.info("新增客服: {}",customerDTO);
        customerService.saveCustomer(customerDTO);

        //TODO 缓存处理

        // 清理缓存数据
        //String key = "dish_" + shopDTO.getCategoryId();
        //cleanCache(key);
        return Result.success();
    }

    /**
     * 批量删除客服信息
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除客服信息")
    public Result delete(@RequestParam List<Long> ids){
        // @RequestParam注解能够让mybatis去解析前端发送过来的字符串，并添加到ids中
        log.info("批量删除客服信息:{}",ids);
        customerService.deleteBatch(ids);

        //TODO 缓存处理

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 根据id查询客服信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询客服信息")
    public Result<CustomerVO> getById(@PathVariable Long id){
        log.info("根据id查询客服信息:{}",id);
        CustomerVO customerVO = customerService.getCustomerById(id);
        return Result.success(customerVO);
    }

    /**
     * 修改客服信息
     * @param customerDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改客服信息")
    public Result update(@RequestBody CustomerDTO customerDTO){
        log.info("修改客服信息:{}",customerDTO);
        customerService.updateCustomer(customerDTO);

        //TODO 缓存处理

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 启用禁用客服
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用客服")
    public Result startOrStop(Long id,@PathVariable Integer status){
        log.info("启用禁用客服:{},{}",id,status);
        customerService.startOrStopCustomer(id,status);

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }
}
